package com.sinoglobal.sinostore.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.bean.AddressVo;
import com.sinoglobal.sinostore.dialog.ShopDialogStyle3;
/**
 * 
* @title: ZiquAddressAdapter.java 
* @package com.chiliring.sinostore.adapter 
* @description: TODO(自取地址Adapter) 
* @author LK
* @date 2015-1-16 下午2:16:42
 */
public class ZiquAddressAdapter extends BaseAdapter {
	private Context context;
	private List<AddressVo> data;
	
	public ZiquAddressAdapter(Context context,List<AddressVo> data){
		this.context = context;
		this.data = data;
	}
	

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MyOnCilckListener cilckListener = new MyOnCilckListener(position);
		if(null == convertView){
			convertView = LayoutInflater.from(context).inflate(R.layout.shop_item_ziqu_address, null);
		}
		TextView tvAddress = ViewHolder.getViewById(convertView, R.id.tv_address);
		TextView tvTime = ViewHolder.getViewById(convertView, R.id.tv_time);
		TextView tvContact = ViewHolder.getViewById(convertView, R.id.tv_contact);
		TextView tvContactPhone = ViewHolder.getViewById(convertView, R.id.tv_contact_phone);
		TextView tvNote = ViewHolder.getViewById(convertView, R.id.tv_note);
		LinearLayout llCallPhone = ViewHolder.getViewById(convertView, R.id.ll_callphone);
		AddressVo entity = (AddressVo)getItem(position);
		tvAddress.setText(String.format("地址：%s", entity.getAddress()));
		tvTime.setText(String.format("时间：%s", entity.getDraw_time()));
		tvContact.setText(String.format("联系人：%s", entity.getConnect_name()));
		tvContactPhone.setText(String.format("电话：%s", entity.getConnect_tel()));
		tvNote.setText(String.format("备注：%s", entity.getNote()));
		llCallPhone.setOnClickListener(cilckListener);
		return convertView;
	}
	
	private class MyOnCilckListener implements OnClickListener{
		private int position;
		public MyOnCilckListener(int position){
			this.position = position;
		}

		@Override
		public void onClick(View v) {
			AddressVo entity = (AddressVo)getItem(position);
			if(!TextUtils.isEmpty(entity.getConnect_tel())){
				String phones[] = entity.getConnect_tel().split(",");
				ShopDialogStyle3 dialogStyle3 = null;
				if(phones.length == 1){
					Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+phones[0].trim())); 
					context.startActivity(intent);
				}else{
					dialogStyle3 = new ShopDialogStyle3(context, phones[0], phones[1]);
					dialogStyle3.show();
				}
			}
		}
	}
}
